<?php
namespace app\common\service;


use think\Db;
use think\Model;

class CampusService extends BaseService
{

    /**
     * 新增校区
     * @param $sid
     * @param $name
     * @return int
     */
    public function addCampus($sid, $name){
        $data = [
            'sid' => $sid,
            'name' => $name,
            'add_time' => time(),
            'status' => 1
        ];
        return db('campus')->insertGetId($data);
    }


    /**
     * 编辑校区
     * @param $sid
     * @param $id
     * @param $name
     * @throws \think\Exception
     */
    public function editCampus($sid, $id, $name){
        return db('campus')->where(['id' => $id, 'sid' => $sid])->update(['name' => $name]);
    }


    /**
     * 删除校区
     * @param $id
     * @return int
     */
    public function deleteCampus($sid, $id){
        //查询是否有班级已经绑定校区
        $classes = db('classes')->where(['sid' => $sid, 'campus_id' => $id])->find();
        if($classes){
            return ['status' => 0, 'msg' => '已有班级绑定该校区，无法删除'];
        }

        $result = db('campus')->where(['id' => $id])->setField('status', -1);

        return $result !== false ? ['status' => 1, 'msg' => '删除成功'] : ['status' => 0, 'msg' => '删除失败'];
    }


    /**
     * 获取校区列表
     * @param $sid
     * @return mixed
     */
    public function getCampusList($sid){
        $list = db('campus')->field('id,name')->where(['sid' => $sid, 'status' => 1])->order('id DESC')->select();
        $data['content'] = $list;
        return $data;
    }

}